﻿using lucky_dog_backend.Models;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace lucky_dog_backend.Services
{
    public class LuckyTableService
    {
        private readonly IMongoCollection<LuckyTable> _luckyTables;

        public LuckyTableService(ILuckyDogDatabaseSettings settings)
        {
            var client = new MongoClient(settings.ConnectionString);
            var database = client.GetDatabase(settings.DatabaseName);
            _luckyTables = database.GetCollection<LuckyTable>(settings.LuckyTablesCollectionName);
        }

        public async Task<List<LuckyTable>> Get() =>
            await _luckyTables.Find(luckyTable => true).ToListAsync();

        public async Task<LuckyTable> Get(string id) =>
            await _luckyTables.Find(luckyTable => luckyTable.Id == id).FirstOrDefaultAsync();

        public async Task Create(LuckyTable luckyTable)
        {
            await _luckyTables.InsertOneAsync(luckyTable);
        }

        public async Task Remove(string id) =>
           await _luckyTables.DeleteOneAsync(luckyTable => luckyTable.Id == id);
        public async Task<List<LuckyTable>> GetUserTable(User user) => await GetUserTable(user.Id);
        public async Task<List<LuckyTable>> GetUserTable(string id)
        {
            var filter = Builders<LuckyTable>.Filter.Eq("UserId", id);
            return await _luckyTables.Find(filter).ToListAsync();
        }
    }
}
